define(function (require, exports, module) {
    require("apps/grid");
    require("apps/easyui");

    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "device_template"
            },
            configs: {
                gridTitle: "设备列表",
                gridUrl: ctx + "/app/api/device/findDevices",
                gridColumns: [
                    {field: "id", checkbox: true},
                    {field: "deviceCode", title: "设备编号", width: 100},
                    {field: "deviceName", title: "设备名称", width: 100},
                    {field: "deviceCover", title: "设备图片", hidden:true, formatter: function(value, row, index){
                        var hr = ctx  + "/print/" + value;
                        return "<img src='"+ hr +"' style=\"width:48px;height:32px;\"/>"
                    }},
                    {field: "manufacturer", title: "生产厂家", width: 200},
                    {field: "purchaseDate", title: "购买日期", width: 100},
                    {field: "purchaseAmount", title: "购买金额", width: 100},
                    {field: "keyParameter", hidden:true, title: "主要参数", width: 100},
                    {field: "repairRecord", hidden:true, title: "维修记录", width: 200},
                    {field: "operators", title: "操作者", width: 100},
                    {field: "parserDevice", title: "程控设备", width: 100, formatter: function (value, row, index) {
                        return value == "0" ? "否" : "是";
                    }}
                ]
            },
            events: {
                renderColBarBefore: function (btn, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "edit":
                            whenEdit(context, btn.href, row);
                            break;
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "设备搜索", "device_search");
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        case "del":
                            context.del(btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();

        function whenEdit(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "device_update_save",
                title: "编辑设备",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            context.detail("设备详情", row);
        }

        function whenAdd(context, href) {
            globalWindow.create({
                href: href,
                method: "device_add_save",
                title: "新增设备",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

    };

    require("apps/file");
    exports.add = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid);
//        var da =  page.getJqDom("da");
        page.getJqDom("purchaseDate").datebox({
            require:true
        });
        page.getJqDom("parserDevice").toggleButton({
            dataRef:{
                "0":"否",
                "1":"是"
            },
            onCheck:function(value){
                if(value == "1"){
                    page.getJqDom("parser-box").show()
                }else{
                    page.getJqDom("parser-box").hide();
                }
            }
        });
        page.getJqDom("parser").combobox({
            editable:false
        });
        page.getJqDom('uploadCover').baseImageUpload({
            'uploader' : ctx + '/app/api/file/uploadTempFile'
        });

        window.device_add_save = function(win) {
            var postForm = page.getJqDom("form-device-info");
            if (!postForm.form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var postData = postForm.serializeJson();
            if(postData.parserDevice == 1 && !postData.parser){
                $.messager.alert('信息',
                    "程控设备至少需要选择一个实验数据解析器", 'error');
                return;
            }
            if(postData.parserDevice == 0){
                delete postData.parser;
            }
            $.post(postForm.attr("action") + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (data) {
                    win.wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.update = function(ctx, uuid, config){
        var page = new $.PageDomFactory(uuid);
        page.getJqDom("purchaseDate").datebox({
            require:true
        });
        page.getJqDom("parser").combobox({
            value:config.parser,
            editable:false
        });
        page.getJqDom('uploadCover').baseImageUpload({
            'uploader' : ctx + '/app/api/file/uploadTempFile'
        });
        page.getJqDom("parserDevice").toggleButton({
            dataRef:{
                "0":"否",
                "1":"是"
            },
            onCheck:function(value){
                if(value == "1"){
                    page.getJqDom("parser-box").show()
                }else{
                    page.getJqDom("parser-box").hide();
                }
            }
        });

        window.device_update_save = function(win) {
            var postForm = page.getJqDom("form-device-info");
            if (!postForm.form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var postData = postForm.serializeJson();
            if(postData.parserDevice == 1 && !postData.parser){
                $.messager.alert('信息',
                    "程控设备至少需要选择一个实验数据解析器", 'error');
                return;
            }
            if(postData.parserDevice == 0){
                delete postData.parser;
            }
            $.post(postForm.attr("action") + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (data) {
                    win.wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.search = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid, true);
        page.getJqDom("parserDevice").combobox({
            editable: false
        });

        window.device_search =  function(win) {
            win.wait("start", "搜索中,请稍候.");
            var form = page.getJqDom("form-device-info");
            var postData = form.serializeJson();
            win.callback(postData);
            win.close();
        }
    }
});